package com.lysj.bhp.system.service;

import com.lysj.bhp.system.base.BaseConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;

@Service
public class MailService {

    private static final Logger logger = LoggerFactory.getLogger(MailService.class);

    @Resource
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String sender; //读取配置文件中的参数

    public boolean sendSimpleMail(String sendTo, String titel, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(sender); // 邮件的发送者
        message.setTo(sendTo);
        message.setSubject(titel);
        message.setText(content);
        mailSender.send(message);
        return true;
    }

    public boolean sendForgetPasswordHtmlMail(String sendTo, String forgetCode) {
        MimeMessage message = null;
        try {
            message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(sender);
            helper.setTo(sendTo);
            helper.setSubject("Forget Password - BHPC"); //标题

            String forgetUrl = BaseConst.DOMAIN + "/bhp/reset-password.html?forgetCode=" + forgetCode;


            String content = " <p style=\"font-size: 18px;color: rgb(166,166,166)\">Forget Password</p>\n" +
                    "    <div style=\"color: rgb(93,93,93)\">\n" +
                    "        <a style=\"margin:15px auto auto 15px;font-size:14px;text-align:center;border: 1px solid rgb(229,229,229);height:30px;line-height:30px;width:160px;background-color: rgb(245,245,245)\" href=\" " + forgetUrl + " \">\n" +
                    "            Click!\n" +
                    "        </a>\n" +
                    "        <div style=\"margin-top: 15px\">\n" +
                    "            Please click this link or paste the address into your browser to change password:\n" +
                    "            <a href=\" " + forgetUrl + "\">\n" +
                    "                 " + forgetUrl + "  \n" +
                    "            </a>\n" +
                    "        </div>\n" +
                    "\n" +
                    "    </div>\n" +
                    "    <div style=\"color: rgb(166,166,166);margin-top: 15px\">\n" +
                    "        <div>Regards,\n" +
                    "        </div>\n" +
                    "        <div>The BHP Team\n" +
                    "        </div>\n" +
                    "        <div>\n" +
                    "            <a href=\"https://www.bhp.finance\">www.bhp.finance</a>\n" +
                    "        </div>\n" +
                    "    </div>";


            helper.setText(content, true);
            mailSender.send(message);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Send Email error:" + e.getMessage());
            return false;
        }
    }

    public boolean sendRegistSuccessHtmlMail(String sendTo, String activeCode) {
        MimeMessage message = null;
        try {
            message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(sender);
            helper.setTo(sendTo);
            helper.setSubject("Welcome - BHPC"); //标题

            String activeUrl = BaseConst.DOMAIN + "/bhp/wait.html?activeCode=" + activeCode;

            String content = "<div style=\"margin-left: 20px\">\n" +
                    "    <p style=\"color:rgb(153,96,96);\">\n" +
                    "    <p>Welcome to BHP!</p>\n" +
                    "    <p>Welcome and thank you for signing up. Click the link below to complete verification:</p>\n" +
                    "    <div>\n" +
                    "        Please click this link or paste the address into your browser to confirm your registration: " +
                    "            <a href=\" " + activeUrl + "\">\n" +
                    "                 " + activeUrl + "  \n" +
                    "            </a>\n" +
                    "    </div>\n" +
                    "    </p>\n" +
                    "\n" +
                    "    <p>After you verify your registration,</p>\n" +
                    "    <p>You will finish registering BHP account, and be ready to make your first deposit and start investing.</p>\n" +
                    "    <p>If you have any questions, please be sure to check out our website for online chat. If you still have questions, don't hesitate to contact us at <a\n" +
                    "            href=\"info@bhpcash.io.\">info@bhpcash.io.</a></p>\n" +
                    "    <p>Check out our <a href=\"//twitter.com/BHPFinance\" target=\"_blank\">Twitter</a> for announcements, helpful guides, and change logs detailing all the new changes we've made to the platform. You can chat with other investors in <a href=\"//t.me/bhpfinance\" target=\"_blank\">Telegram</a>! You can also follow us on <a href=\"//www.instagram.com/bhp_finance/\" target=\"_blank\">Iinstagram</a> and subscribe to our <a href=\"//www.facebook.com/BHPFinance/\" target=\"_blank\">Facebook</a> to stay up to date with everything going on at BHP. </p>\n" +
                    "    <div style=\"margin-top: 50px\">\n" +
                    "        <p>Regards, </p>\n" +
                    "        <p>The BHP Team</p>\n" +
                    "        <p><a href=\"//www.bhp.finance\">www.bhp.finance</a></p>\n" +
                    "    </div>\n" +
                    "</div>";


            helper.setText(content, true);
            mailSender.send(message);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Send Email error:" + e.getMessage());
            return false;
        }
    }

    public boolean sendVerifyCodeHtmlMail(String sendTo, String activeCode) {
        MimeMessage message = null;
        try {
            message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(sender);
            helper.setTo(sendTo);
            helper.setSubject("Verify Code - BHPC"); //标题

            String activeUrl = BaseConst.DOMAIN + "bhp/html/EN/wait.html?activeCode=" + activeCode;

            String content = "<p style=\"font-size: 18px;color: rgb(166,166,166)\">Withdraw Confirmation</p>\n" +
                    "    <div style=\"color: rgb(93,93,93)\">\n" +
                    "        <div style=\"margin-top: 15px\">\n" +
                    "            A withdraw has been credited to your account.\n" +
                    "        </div>\n" +
                    "        <div style=\"margin-top: 15px\">\n" +
                    "            If you did not initial this transaction, please contact us at support@bhfinance.com\n" +
                    "        </div>\n" +
                    "        <div style=\"margin:15px auto;text-align: center;font-size: 24px;font-weight:bold;color: rgb(197,147,77);height: 30px;width:140px;background-color: rgb(233,233,233)\">\n" +
                    "            " + activeCode + "\n" +
                    "        </div>\n" +
                    "    </div>\n" +
                    "    <div style=\"color: rgb(166,166,166);margin-top: 15px\">\n" +
                    "        <div>Regards,\n" +
                    "        </div>\n" +
                    "        <div>The BHP Team\n" +
                    "        </div>\n" +
                    "        <div>\n" +
                    "            <a href=\"http://www.bhp.finance\">www.bhp.finance</a>\n" +
                    "        </div>\n" +
                    "    </div>";


            helper.setText(content, true);
            mailSender.send(message);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Send Email error:" + e.getMessage());
            return false;
        }
    }

}
